﻿using MvvmFx.Common.ViewModels;
using MvvmFx.Common.ViewModels.Behaviors.ChangeTracking;
using MvvmFx.Common.ViewModels.Behaviors.LoadingSaving;

namespace MvvmFx.Samples.HowTos.ChangeTracking.ViewModels
{
    public class ContactDetailsViewModel : ViewModelBase,
                                           ILoadSaveable,
                                           IChangeTrackable
    {
        #region Fields

        private readonly IViewModelLoader<ContactDetailsViewModel> _loader;
        private readonly IViewModelSaver<ContactDetailsViewModel> _saver;
        private readonly SaveCommand _saveCommand;
        private readonly ChangeService _changeService;

        private string _emailAddress;
        private string _telNumber;
        private AddressViewModel _address;

        #endregion

        #region Constructor

        public ContactDetailsViewModel(IViewModelLoader<ContactDetailsViewModel> loader, IViewModelSaver<ContactDetailsViewModel> saver)
        {
            _loader = loader;
            _saver = saver;
            _changeService = new ChangeService(this, TrackingMode.TrackChildren);

            _saveCommand = new SaveCommand(this);
        }

        #endregion

        #region Collection Properties

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the value of the <see cref=”EmailAddress”/> property.
        /// </summary>
        public string EmailAddress
        {
            get { return _emailAddress; }
            set
            {
                if (_emailAddress != value)
                {
                    _emailAddress = value;
                    OnPropertyChanged("EmailAddress");
                }
            }
        }

        /// <summary>
        /// Gets or sets the value of the <see cref=”TelNumber”/> property.
        /// </summary>
        public string TelNumber
        {
            get { return _telNumber; }
            set
            {
                if (_telNumber != value)
                {
                    _telNumber = value;
                    OnPropertyChanged("TelNumber");
                }
            }
        }

        /// <summary>
        /// Gets or sets the value of the <see cref=”Address”/> property.
        /// </summary>
        public AddressViewModel Address
        {
            get { return _address; }
            set
            {
                if (_address != value)
                {
                    _address = value;
                    OnPropertyChanged("Address");
                }
            }
        }

        #endregion

        #region Public Commands

        /// <summary>
        /// Gets the value of the <see cref=”SaveCommand”/> property.
        /// </summary>
        public SaveCommand SaveCommand
        {
            get { return _saveCommand; }
        }

        #endregion

        #region ILoadSaveable Members

        public void Load()
        {
            _loader.Load(this);
        }

        /// <summary>
        /// Saves this ViewModel object
        /// </summary>
        public void Save()
        {
            if (_saver != null)
            {
                _saver.Save(this);
            }
        }

        #endregion

        #region IChangeTrackable Members

        public ChangeService ChangeService
        {
            get { return _changeService; }
        }

        #endregion
    }
}